/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PCustomFaceServer.java
 *
 * Created on Apr 20, 2012, 2:31:30 PM
 */
package com.bc5Neptune.cis.gui;

import com.bc5Neptune.cis.bll.IconList;
import com.bc5Neptune.cis.bll.ProcessImage;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;

/**
 *
 * @author phu.huynh
 */
public class PCustomFaceServer extends javax.swing.JPanel {

    /** Creates new form PCustomFaceServer */
    private int pointCount = 0;
    private Point points[] = new Point[100];
    private Point points2[] = new Point[100];
    private Point start = new Point();
    private Point end = new Point();
    Rectangle rect = new Rectangle();
    BufferedImage image;



    public void setImage(BufferedImage image) {
        this.image = image;
    }

    public PCustomFaceServer() {
        
        initComponents();
        setCursor(new Cursor(1));//set cut cursor
        addMouseMotionListener(new MouseMotionAdapter() {

            @Override
            public void mouseDragged(MouseEvent ev) {
                end = ev.getPoint();
                rect.setFrameFromDiagonal(start, end);
                repaint();
            }//end mouse drag
        });
        addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                start = e.getPoint();
            }

            @Override
            public void mouseReleased(MouseEvent ev) {
                points[pointCount] = start;
                points2[pointCount] = ev.getPoint();
                pointCount++;
                rect.setFrameFromDiagonal(start, start);
                repaint();
                
                
                //update face into custom list
                //creat a rectangle
                Rectangle r = new Rectangle();
                r.setFrameFromDiagonal(start, end);
                BufferedImage bufferedImage = image
                        .getSubimage(r.x, r.y, r.width, r.height);
                bufferedImage = new ProcessImage().resize(bufferedImage, 92, 112);
                IconList icon = new IconList("Custom Face",bufferedImage, 92, 112);
                PServer.customFaceArr.add(icon);
                PServer.renderCustomFace();
                
            }
        });
    }

    protected void paintComponent(Graphics g) {
        //draw image
        super.paintComponent(g);
        if (image != null) {
            g.drawImage(image, 0, 0, this);
            System.out.println("Draw a image");
        }
        
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        // Draw line being dragged.
        g2.setPaint(Color.red);
        g2.draw(rect);
        // Draw lines between points in arrays.
        g2.setPaint(Color.blue);
        Rectangle r = new Rectangle();
        for (int i = 0; i < pointCount; i++) {
            r.setFrameFromDiagonal(points[i], points2[i]);
           // g2.fill(r);
        }
    }

    public static void main(String[] args) {
        PCustomFaceServer test = new PCustomFaceServer();
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(test);
        f.setSize(400, 400);
        f.setLocation(200, 200);
        f.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 597, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 457, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
